Skip to content

fix(logger): correctly refresh sample rate #3722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 13, 2025
Merged

Conversation

dreamorosi
Copy link
Contributor

Summary

Changes

Please provide a summary of what's being changed

This PR refactors the internal logic to calculate and refresh log sampling so that it works for any usage pattern. In #3278 we introduced changes that automatically refresh the sample rate calculation when using the injectLambdaContext() class method decorator and Middy.js middleware, but broke the manual method in the process.

The PR fixes the bug reported by a customer in the linked issue.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: fixes #3718


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi requested a review from am29d March 13, 2025 14:26
@dreamorosi dreamorosi self-assigned this Mar 13, 2025
@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Mar 13, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Mar 13, 2025
@github-actions github-actions bot added the bug Something isn't working label Mar 13, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 13, 2025
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix of bug. I am still a bit puzzled about the cause and also the solution. I see that we have now two fields to capture sampleRate value but use only one of the for the refresh decision.

You have also decoupled the coldStart into a dedicated counter to check if we are in a warm state or now.

Can you elaborate on the solution? Is that correct that refreshSampleRate is skipped because of the order we read env variable last and because we are passing a cached constructor value, which is 0?

@dreamorosi
Copy link
Contributor Author

The previous implementation was checking the cold start in two places:

  • when adding context
  • when refreshing the cold start

Only the first one flipped the state, but when refreshing the sample rate manually and not using the context (as the example in the linked issue) the state never got flipped and so the method always returned early.

You can verify this by pulling the main branch and adding a test that does this:

  • initialize logger w sample rate
  • call refresh sample rate manually
  • call refresh sample rate manually

If you debug the test and step through (or just put a breakpoint in the method) you'll see it never calls the sample rate refresh internally.

Either way, in hindsight, we shouldn't have relied on the set initial sample rate method to refresh it either, since there's a lot of logic that we don't need to run every time.

Now it's the other way around, and we keep a dedicated state for the sample rate config: one to keep track of the actual rate (which was stored in a nested object that didn't belong to it) and another to keep track of how many times it's been called.

@am29d am29d merged commit 2692ca4 into main Mar 13, 2025
42 checks passed
@am29d am29d deleted the fix/manual_log_sampling branch March 13, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation logger This item relates to the Logger Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: manually calling refreshSampleRateCalculation does not refresh the sampling decision
2 participants